package com.unidt.www.module.bpm.controller.admin.task;

import com.unidt.www.framework.common.pojo.CommonResult;
import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.module.bpm.api.task.dto.BpmProcessInstanceCreateEXTDTO;
import com.unidt.www.module.bpm.controller.admin.task.vo.task.*;
import com.unidt.www.module.bpm.dal.dataobject.definition.BpmFormDO;
import com.unidt.www.module.bpm.dal.dataobject.task.BpmProcessTaskExtDO;
import com.unidt.www.module.bpm.dto.BpmDynamicAssignDto;
import com.unidt.www.module.bpm.service.task.BpmTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.flowable.bpmn.model.UserTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.unidt.www.framework.common.pojo.CommonResult.success;
import static com.unidt.www.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Api(tags = "管理后台 - 流程任务实例")
@RestController
@RequestMapping("/bpm/task")
@Validated
public class BpmTaskController {

    @Resource
    private BpmTaskService taskService;

    @GetMapping("todo-page")
    @ApiOperation("获取 Todo 待办任务分页")
    @PreAuthorize("@ss.hasPermission('bpm:task:query')")
    public CommonResult<PageResult<BpmTaskTodoPageItemRespVO>> getTodoTaskPage(@Valid BpmTaskTodoPageReqVO pageVO) {
        return success(taskService.getTodoTaskPage(getLoginUserId(), pageVO));
    }

    @GetMapping("done-page")
    @ApiOperation("获取 Done 已办任务分页")
    @PreAuthorize("@ss.hasPermission('bpm:task:query')")
    public CommonResult<PageResult<BpmTaskDonePageItemRespVO>> getDoneTaskPage(@Valid BpmTaskDonePageReqVO pageVO) {
        return success(taskService.getDoneTaskPage(getLoginUserId(), pageVO));
    }

    @GetMapping("/list-by-process-instance-id")
    @ApiOperation(value = "获得指定流程实例的任务列表", notes = "包括完成的、未完成的")
    @ApiImplicitParam(name = "processInstanceId", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('bpm:task:query')")
    public CommonResult<List<BpmTaskRespVO>> getTaskListByProcessInstanceId(
        @RequestParam("processInstanceId") String processInstanceId) {
        return success(taskService.getTaskListByProcessInstanceId(processInstanceId));
    }

    @PutMapping("/approve")
    @ApiOperation("通过任务")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<Boolean> approveTask(@Valid @RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.approveTask(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reject")
    @ApiOperation("不通过任务")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<Boolean> rejectTask(@Valid @RequestBody BpmTaskRejectReqVO reqVO) {
        taskService.rejectTask(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-assignee")
    @ApiOperation(value = "更新任务的负责人", notes = "用于【流程详情】的【转派】按钮")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<Boolean> updateTaskAssignee(@Valid @RequestBody BpmTaskUpdateAssigneeReqVO reqVO) {
        taskService.updateTaskAssignee(getLoginUserId(), reqVO);
        return success(true);
    }

    @PostMapping("/addSignTask")
    @ApiOperation(value = "加签接口", notes = "用于【流程详情】的【加签】按钮")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<String> addSignTask(@Valid @RequestBody BpmTaskSignVO reqVO) {
        return taskService.addSignTask(reqVO);
    }

    @PostMapping("/fallback")
    @ApiOperation(value = "回退", notes = "用于【流程详情】的【回退】按钮")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<String> fallback(@Valid @RequestBody BpmTaskBackVO  backVO) {

         return taskService.fallback(backVO);
    }



    @PostMapping("/getTaskBpmForm")
    @ApiOperation(value = "获取流程节点表单json接口", notes = "用于【流程节点】的表单回显功能")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<BpmFormDO> getTaskBpmForm(@RequestParam("taskId") String taskId) {

        return taskService.getTaskBpmForm(taskId);
    }

    @PostMapping("/createTaskIdForFormKeyInfo")
    @ApiOperation(value = "保存流程节点表单数据", notes = "用于【流程节点】的表单保存数据")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<String> createTaskIdForFormKeyInfo(@RequestParam("userId") Long userId,@RequestParam("taskId")  String taskId,
                                                           @Valid @RequestBody BpmProcessInstanceCreateEXTDTO createReqDTO) {

        return taskService.createTaskIdForFormKeyInfo(userId,taskId,createReqDTO);
    }

    @PostMapping("/getByTaskIdFormInfo")
    @ApiOperation(value = "流程节点表单回显接口", notes = "用于【流程节点】的表单回显功能")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<BpmProcessTaskExtDO> getByTaskIdFormInfo(@RequestParam("userId") Long userId, @RequestParam("taskId") String taskId) {

        return taskService.getByTaskIdFormInfo(userId,taskId);
    }

    @PostMapping("/automationBpmbyOver")
    @ApiOperation(value = "通过流程实例id自动走完全流程", notes = "通过流程实例id自动走完全流程")
    @PreAuthorize("@ss.hasPermission('bpm:task:update')")
    public CommonResult<String> automationBpmbyOver(@RequestParam("id") String id) {

        return taskService.automationBpmbyOver(id);
    }


    @PostMapping("/getNextTask/{taskId}")
    @ApiOperation(value = "获取下一个审批节点", notes = "获取下一个审批节点")
    @PreAuthorize("@ss.hasPermission('bpm:task:query')")
    public CommonResult<List<UserTask>> getNextTask(@PathVariable("taskId") String taskId) {

        return CommonResult.success(taskService.getNextTask(taskId));
    }






}
